<?php

namespace App\Models;


use App\Tool\Helper;
use Illuminate\Support\Facades\DB;

/**
 * App\Models\Ocr
 *
 * @property int $id
 * @property int|null $user_id 用户id
 * @property int|null $group_id 分组id
 * @property string|null $img 检查单图片
 * @property string|null $name 姓名
 * @property string|null $sex_name 性别
 * @property int|null $age 年龄
 * @property string|null $check_jg 检查机构
 * @property string|null $check_time 检查时间
 * @property string|null $pro_name 判定项目（血常规、便常规、尿常规）
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|Ocr newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Ocr newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Ocr query()
 * @method static \Illuminate\Database\Eloquent\Builder|Ocr whereAge($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Ocr whereCheckJg($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Ocr whereCheckTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Ocr whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Ocr whereGroupId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Ocr whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Ocr whereImg($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Ocr whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Ocr whereProName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Ocr whereSexName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Ocr whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Ocr whereUserId($value)
 * @mixin \Eloquent
 */
class Ocr extends Base{

    protected $table = 'ocr';
    public $columnsArr = [
        'name' => '姓名',
        'sex_name' => '性别',
        'age' => '年龄',
        'check_jg' => '检查机构',
        'check_time' => '检查时间',
        'pro_name' => '判定项目',
    ];

    protected $fillable = [
    ];

//    public $timestamps = false;//关闭自动维护

    /**
     * 保存分析数据到表
     *
     * @param $basic
     * @param $item
     * @return mixed
     */
    public function add($basic, $item, $img = null)
    {
        try {
            DB::beginTransaction();

            // 基础数据写入
            $columnsArr = Helper::reverseArr($this->columnsArr);
            $insertBasic = [];
            foreach ($basic as $k => $value) {
                $insertBasic[$columnsArr[$value['name']] ?? $value['name']] = $value['content'];
            }
            $insertBasic['img'] = $img;
            $insertBasic['img_md5'] = md5(file_get_contents(public_path($img)));
            $insertBasic['created_at'] = date('Y-m-d H:i:s');
            $insertBasic['updated_at'] = $insertBasic['created_at'];
            $insertBasic['group_id'] = $insertBasic['group_id'] ?: null;
            $insertBasic['check_time'] = $insertBasic['check_time'] == '-' ? null : $insertBasic['check_time'];
            $insertBasic['age'] = (int)str_replace('岁', '', $insertBasic['age']);
            $Ocr = new self();
            $Ocr->setRawAttributes($insertBasic);
            $Ocr->save();

            // 扩展信息写入 、、
            $itemExt = [
                ['name' => 'user_id', 'content' => $Ocr->user_id],
                ['name' => 'ocr_id', 'content' => $Ocr->id]
            ];
            $item = array_merge($item, $itemExt);
            if ($insertBasic['pro_name'] == '血常规') {
                (new OcrBlood())->add($item);
            } elseif ($insertBasic['pro_name'] == '便常规') {
                (new OcrThen())->add($item);
            } elseif ($insertBasic['pro_name'] == '尿常规') {
                (new OcrUrine())->add($item);
            }

            DB::commit();
            return $Ocr->id;
        } catch (\Exception $exception) {
            DB::rollBack();
            \Log::error($exception);
            return false;
        }
    }
}
